const express = require("express")
const router = express.Router()
const personalDB = require("../../../db/personal")

// 获取数据
router.get("/getInfo",async(req,res)=>{
  try {
    let doc = await personalDB.findOne({sex:"女"})
    res.send({
      code:0,
      mes:"个人档信息",
      data:doc
    })
  } catch (error) {
    res.send({
      code:5,
      mes:"服务器异常"
    })
  }
})

// 权限判断
router.use((req,res,next)=>{
  if(!req.session.userInfo.allowUpdate){
    return res.send({
      code:15,
      mes:"您没有修改权限，请联系超级管理员进行添加"
    })
  }
  next()
})

// 修改个人档
router.post("/update",async (req,res)=>{
  try {
    let {type} = req.body
    if(!type){
      let {personalForm} = req.body
      await personalDB.findByIdAndUpdate("6140ae0a6a3fff078156e16f",{
        sex:personalForm.sex,
        blood:personalForm.blood,
        age:personalForm.age,
        home:personalForm.home,
        birth:personalForm.birth,
        occupation:personalForm.occupation,
        constellation:personalForm.constellation,
        corporate:personalForm.corporate,
        residence:personalForm.residence,
        country:personalForm.country,
        marital:personalForm.marital,
        address:personalForm.address,
      })
    }else{
      let {moreForm} = req.body
      await personalDB.findByIdAndUpdate("6140ae0a6a3fff078156e16f",{
        color:moreForm.color,
        star:moreForm.star,
        motion:moreForm.motion,
        drinks:moreForm.drinks,
        num:moreForm.num,
        spot:moreForm.spot,
        song:moreForm.song,
        dress:moreForm.dress,
        foods:moreForm.foods,
        film:moreForm.film,
        comic:moreForm.comic,
        pets:moreForm.pets
      })
    }
    // await personalDB.create({
    //   sex:"女",
    //   blood:"女",
    //   age:20,
    //   home:"女",
    //   birth:"女",
    //   occupation:"女",
    //   constellation:"女",
    //   corporate:"女",
    //   residence:"女",
    //   country:"女",
    //   marital:"女",
    //   address:"女",
    //   color:"女",
    //   star:"女",
    //   motion:"女",
    //   drinks:"女",
    //   num:20,
    //   spot:"女",
    //   song:"女",
    //   dress:"女",
    //   foods:"女",
    //   film:"女",
    //   comic:"女",
    //   pets:"女"
    // })
   
    res.send({
      code:0,
      mes:"数据更新成功"
    })
  } catch (error) {
    console.log(error)
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})



module.exports = router